//
//  ViewController.m
//  UI23_01_3rd_SDWebImage使用
//
//  Created by 李劲成 on 16/4/11.
//  Copyright © 2016年 ljc. All rights reserved.
//

#import "ViewController.h"
#import "NetworkHandler.h"
#import "ModelOfNews.h"
#import "UIImageView+WebCache.h"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray *arrOfNews;

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self handleData];
    // Do any additional setup after loading the view, typically from a nib.
}

#pragma mark - tableView DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arrOfNews.count;
}
#pragma mark - Data
- (void)handleData {
    self.arrOfNews = [NSMutableArray array];
    [NetworkHandler networkHandlerGetWithURL:@"http://c.m.163.com/nc/article/headline/T1348647853363/0-20.html?from=toutiao&passport=&devId=x5%2BSswSS0UhQ6mO8rpqsSoLg4r76JP0mXhMHVg5DE4uh60X8TbfN6XM9ludBdhOC&size=20&version=5.6.0&spever=false&net=wifi&lat=7eW%2F5%2F8o5hlEi%2Fot5rNTsw%3D%3D&lon=d4Nvc%2FwkfS6tuLYTwGx4Dw%3D%3D&ts=1460108771&sign=Ren6fX4TjmDWjOsIVtLCAhcXYPtCtK7kOTCU6ve%2F3kt48ErR02zJ6%2FKXOnxX046I&encryption=1&canal=appstore" completionHandler:^(id result, NSURLResponse *response, NSError *error) {
        NSArray *arr = [result objectForKey:@"T1348647853363"];
        for (NSDictionary *dic in arr) {
            
            // Model 获取数据
            ModelOfNews *news = [[ModelOfNews alloc]init];
            [news setValuesForKeysWithDictionary:dic];
            [self.arrOfNews addObject:news];
            
            // tableView 刷新.
            [self.tableView reloadData];
        }
    }];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    ModelOfNews *model = [self.arrOfNews objectAtIndex:indexPath.row];
    
    NSURL *url = [NSURL URLWithString:model.img];
    [cell.imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"tooopen_sy_156324542564.jpg"]];
    return cell;
}
#pragma mark - tableView Delegate


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
